package controller;

import ui.Main;
import common.MessageType;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import util.EmailCheck;
import util.MD5;

import java.io.IOException;

public class LoginControl {
    @FXML
    private Button register;
    //跳转至注册页面
    @FXML
    public void toRegister(ActionEvent event){
        System.out.println("正在前往注册...");
        Stage stage=(Stage) register.getScene().getWindow();
        stage.close();
        Stage rgt=new Stage();
        Pane root= null;
        try {
            root= FXMLLoader.load(getClass().getResource("/Interface/Register.fxml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Scene scene=new Scene(root);
        rgt.setScene(scene);
        rgt.getIcons().add(new Image("/Interface/Icon.png"));
        rgt.setTitle("注册");
        rgt.setResizable(false);
        rgt.show();
    }
    @FXML
    private Button login;
    @FXML
    private TextField zh;
    @FXML
    private PasswordField mm;
    @FXML
    private Label error;
    //点击登录
    @FXML
    public void toLogin(ActionEvent event){
        if(zh.getText().equals("")||mm.getText().equals(""))
        {
            error.setText("请输入账号或密码");
        }else if(!EmailCheck.checkEmail(zh.getText())){
            error.setText("账号格式错误");
        }
        else{
            if(Main.userClientService.UserCheck(zh.getText(), MD5.getPWD(mm.getText()))){
                error.setText("密码正确");
                System.out.println("即将跳转至超星主界面");
                try {
                    Pane root=FXMLLoader.load(getClass().getResource("/Interface/MainInterface.fxml"));
                    ToMain.toMain(login,root);
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("跳转至主界面失败");
                }
                return;
            }else if(Main.userClientService.getMs2().getState()== MessageType.Login_Repeat){
                System.out.println("重复登录");
                error.setText("重复登录");
                return;
            }
            error.setText("账号或密码错误");
        }
    }
    @FXML
    private Button forget;
    @FXML
    public void pwdForget(ActionEvent event){
        Stage stage=new Stage();
        FXMLLoader loader=null;
        Forget changepwd=new Forget();
        Pane root=null;
        try {
            loader= new FXMLLoader(getClass().getResource("/Interface/ForgetPwd.fxml"));
            loader.setController(changepwd);
            root=loader.load();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Scene scene=new Scene(root);
        stage.setScene(scene);
        stage.getIcons().add(new Image("/Interface/Icon.png"));
        stage.setTitle("忘记密码");
        stage.setResizable(false);
        stage.show();
    }
}
